<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<body>
  <script>

    function prosmiseFn(data){
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve(data);
        }, 1000);
      });
    }

    // 使用generator函数与迭代器封装
    function generator(fn){
      let f = fn();
      next();

      function next(data = null){
        let result = f.next(data);
        if(!result.done){
          result.value.then(res => {
            next(res);
          })
        }
      }
    }

    //generator函数是基于迭代器的可迭代函数，generator函数本身是一个普通函数
    function *fn() {
      let res1 = yield prosmiseFn(1);
      console.log(res1);
      let res2 = yield prosmiseFn(2);
      console.log(res2);
      let res3 = yield prosmiseFn(3);
      console.log(res3)
    }

    generator(fn);
  </script>
</body>
</html>